<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>动态映射 | Elasticsearch: 权威指南 | Elastic</title>
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
</head>
<body>
<div class="main-container">
    <section id="content">
        
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原文地址: <a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/dynamic-mapping.html" rel="nofollow">https://www.elastic.co/guide/cn/elasticsearch/guide/current/dynamic-mapping.html</a>, 版权归 www.elastic.co 所有<br/>
                            英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/dynamic-mapping.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/guide/current/dynamic-mapping.html</a>
                            </div>
                        <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="getting-started.html">基础入门</a></span>
»
<span class="breadcrumb-link"><a href="index-management.html">索引管理</a></span>
»
<span class="breadcrumb-node">动态映射</span>
</div>
<div class="navheader">
<span class="prev">
<a href="root-object.html">« 根对象</a>
</span>
<span class="next">
<a href="custom-dynamic-mapping.html">自定义动态映射 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="dynamic-mapping"></a>动态映射<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/070_Index_Mgmt/35_Dynamic_Mapping.asciidoc">edit</a>
</h2>
</div></div></div>
<p>当 Elasticsearch 遇到文档中以前  未遇到的字段，它用 <a class="xref" href="mapping-intro.html" title="映射"><em>dynamic mapping</em></a> 来确定字段的数据类型并自动把新的字段添加到类型映射。</p>
<p>有时这是想要的行为有时又不希望这样。通常没有人知道以后会有什么新字段加到文档，但是又希望这些字段被自动的索引。也许你只想忽略它们。如果Elasticsearch是作为重要的数据存储，可能就会期望遇到新字段就会抛出异常，这样能及时发现问题。</p>
<p>幸运的是可以用 <code class="literal">dynamic</code> 配置来控制这种行为  ，可接受的选项如下：</p>
<div class="variablelist">
<dl class="variablelist">
<dt>
<span class="term">
<code class="literal">true</code>
</span>
</dt>
<dd>
动态添加新的字段—​缺省
</dd>
<dt>
<span class="term">
<code class="literal">false</code>
</span>
</dt>
<dd>
忽略新的字段
</dd>
<dt>
<span class="term">
<code class="literal">strict</code>
</span>
</dt>
<dd>
如果遇到新字段抛出异常
</dd>
</dl>
</div>
<p>配置参数 <code class="literal">dynamic</code> 可以用在根 <code class="literal">object</code> 或任何 <code class="literal">object</code> 类型的字段上。你可以将 <code class="literal">dynamic</code> 的默认值设置为 <code class="literal">strict</code> , 而只在指定的内部对象中开启它, 例如：</p>
<div class="pre_wrapper lang-sense">
<pre class="programlisting prettyprint lang-sense">PUT /my_index
{
    "mappings": {
        "my_type": {
            "dynamic":      "strict", <a id="CO34-1"></a><i class="conum" data-value="1"></i>
            "properties": {
                "title":  { "type": "string"},
                "stash":  {
                    "type":     "object",
                    "dynamic":  true <a id="CO34-2"></a><i class="conum" data-value="2"></i>
                }
            }
        }
    }
}</pre>
</div>
<div class="sense_widget" data-snippet="snippets/070_Index_Mgmt/35_Dynamic_mapping.json"></div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO34-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>如果遇到新字段，对象 <code class="literal">my_type</code> 就会抛出异常。</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO34-2"><i class="conum" data-value="2"></i></a></p>
</td>
<td align="left" valign="top">
<p>而内部对象 <code class="literal">stash</code> 遇到新字段就会动态创建新字段。</p>
</td>
</tr>
</table>
</div>
<p>使用上述动态映射， 你可以给 <code class="literal">stash</code> 对象添加新的可检索的字段：</p>
<div class="pre_wrapper lang-sense">
<pre class="programlisting prettyprint lang-sense">PUT /my_index/my_type/1
{
    "title":   "This doc adds a new field",
    "stash": { "new_field": "Success!" }
}</pre>
</div>
<div class="sense_widget" data-snippet="snippets/070_Index_Mgmt/35_Dynamic_mapping.json"></div>
<p>但是对根节点对象 <code class="literal">my_type</code> 进行同样的操作会失败：</p>
<div class="pre_wrapper lang-sense">
<pre class="programlisting prettyprint lang-sense">PUT /my_index/my_type/1
{
    "title":     "This throws a StrictDynamicMappingException",
    "new_field": "Fail!"
}</pre>
</div>
<div class="sense_widget" data-snippet="snippets/070_Index_Mgmt/35_Dynamic_mapping.json"></div>
<div class="note admon">
<div class="icon"></div>
<div class="admon_content">
<p>把 <code class="literal">dynamic</code> 设置为 <code class="literal">false</code> 一点儿也不会改变 <code class="literal">_source</code> 的字段内容。 <code class="literal">_source</code> 仍然包含被索引的整个JSON文档。只是新的字段不会被加到映射中也不可搜索。</p>
</div>
</div>
</div>
<div class="navfooter">
<span class="prev">
<a href="root-object.html">« 根对象</a>
</span>
<span class="next">
<a href="custom-dynamic-mapping.html">自定义动态映射 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>